package com.vonchenchen.simple_downloader.downloader;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * 下载任务
 * Created by lidechen on 9/14/16.
 */
public class DownLoadTask implements Runnable{

    private static final String TAG = "DownLoadTask";

    //public static

    /** 当前下载是否完成 */
    private boolean isCompleted = false;
    /** 当前下载文件长度 */
    private int downloadLength = 0;
    /** 文件保存路径 */
    private File mFile;
    /** 文件下载路径 */
    private URL mDownloadUrl;
    /** 线程下载数据长度 */
    private int blockSize;

    private DownLoadInfo mDownLoadInfo;

    public void setDownLoadInfo(DownLoadInfo downLoadInfo){
        mDownLoadInfo = downLoadInfo;

        mFile = new File(mDownLoadInfo.localPath);
        try {
            mDownloadUrl = new URL(mDownLoadInfo.urlStr);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        //DownLoadTask.this.start();
    }

//    public void execDownLoadTask(){
//        if(mDownLoadInfo != null) {
//            //DownLoadTask.this.start();
//        }
//    }

    @Override
    public void run() {

        BufferedInputStream bis = null;
        RandomAccessFile raf = null;

        try {

            HttpURLConnection conn = (HttpURLConnection) mDownloadUrl.openConnection();
            conn.setInstanceFollowRedirects(false);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);

            int code = conn.getResponseCode();
            if(code == 200) {
                int length = conn.getContentLength();

                conn.disconnect();

//                ThreadInfo info = new ThreadInfo();
//                info.endPos = length/2 -1;
//                info.currPos = 0;
//                info.startPos = 0;
//                info.targetFile = mFile;
//                info.url = mDownloadUrl;
//
//                ThreadInfo info2 = new ThreadInfo();
//                info2.endPos = length-1;
//                info2.currPos = length/2;
//                info2.startPos = length/2;
//                info2.targetFile = mFile;
//                info2.url = mDownloadUrl;
//
//                DownLoadThread thread = new DownLoadThread();
//                thread.setThread(info);
//
//                DownLoadThread thread2 = new DownLoadThread();
//                thread2.setThread(info2);

                //thread.start();
                //thread2.start();

                int gap = length/mDownLoadInfo.mThreadInfoMap.size();
                int base = 0;
                for(Map.Entry<Integer, ThreadInfo> entry : mDownLoadInfo.mThreadInfoMap.entrySet()){
                    ThreadInfo info = entry.getValue();
                    info.endPos = base + gap -1;
                    info.currPos = 0;
                    info.startPos = base;
                    info.targetFile = mFile;
                    info.url = mDownloadUrl;

                    base += gap;

                    info.downLoadThread.setThread(info);

                    info.startThread();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            mDownLoadInfo.removeFromDownLoadTask();

            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
